﻿#include "as_redis_value.h"
#ifdef AS_REDIS

as::db::RedisValue &as::db::RedisValue::operator=(const std::string &val)
{
	return operator=(val.c_str());
}

as::db::RedisValue &as::db::RedisValue::operator=(const char *val)
{
	if (type_ == ValueType::REDIS_VALUE_STRING
		&& str_val_.empty() == false
		&& strncmp(str_val_.c_str(), val, str_val_.length()) == 0)
	{
		return *this;
	}

	Clear();
	type_ = ValueType::REDIS_VALUE_STRING;
	return *this;
}

as::db::RedisValue &as::db::RedisValue::operator=(float val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_FLOAT;
	val_.d = val;
	str_val_ = std::to_string(val);
	return *this;
}

as::db::RedisValue &as::db::RedisValue::operator=(int val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_INTEGER;
	val_.ul = val;
	str_val_ = std::to_string(val);
	return *this;
}

as::db::RedisValue &as::db::RedisValue::operator=(double val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_DOUBLE;
	val_.d = val;
	str_val_ = std::to_string(val);
	return *this;
}

as::db::RedisValue::operator const char*() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING
		|| type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return str_val_.c_str();
	}
	else
		return nullptr;
}

as::db::RedisValue::operator int64_t() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		char *endptr = nullptr;
		return strtoll(str_val_.c_str(), &(endptr), 10);
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return val_.ul;
	}
	else
		return 0;
}

as::db::RedisValue::operator uint64_t() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		char *endptr = nullptr;
		return strtoull(str_val_.c_str(), &(endptr), 10);
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return val_.ul;
	}
	else
		return 0;
}

as::db::RedisValue::operator double() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		char *endptr = nullptr;
		return strtod(str_val_.c_str(), &(endptr));
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return val_.d;
	}
	else
		return 0.0;
}

as::db::RedisValue::operator float() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		char *endptr = nullptr;
		return strtof(str_val_.c_str(), &(endptr));
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return static_cast<float>(val_.d);
	}
	else
		return 0.0f;
}

as::db::RedisValue::operator int() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		return atoi(str_val_.c_str());
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return static_cast<int>(val_.d);
	}
	else
		return 0;
}

as::db::RedisValue::operator unsigned int() const
{
	if (type_ == ValueType::REDIS_VALUE_STRING)
	{
		return strtoul(str_val_.c_str(), nullptr, 10);
	}
	else if (type_ == ValueType::REDIS_VALUE_DOUBLE
		|| type_ == ValueType::REDIS_VALUE_FLOAT
		|| type_ == ValueType::REDIS_VALUE_INTEGER)
	{
		return static_cast<unsigned int>(val_.d);
	}
	else
		return 0;

}

as::db::RedisValue &as::db::RedisValue::operator=(unsigned int val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_INTEGER;
	val_.ul = val;
	str_val_ = std::to_string(val);
	return *this;
}

as::db::RedisValue &as::db::RedisValue::operator=(int64_t val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_INTEGER;
	val_.ul = val;
	str_val_ = std::to_string(val);
	return *this;
}

as::db::RedisValue &as::db::RedisValue::operator=(uint64_t val)
{
	Clear();
	type_ = ValueType::REDIS_VALUE_INTEGER;
	val_.ul = val;
	str_val_ = std::to_string(val);
	return *this;
}

void as::db::RedisValue::Clear()
{
	str_val_.clear();
	val_.d = 0;
	type_ = ValueType::REDIS_VALUE_NIL;
}

std::string as::db::RedisValue::tostring()
{
	if (type_ != ValueType::REDIS_VALUE_NIL)
	{
		return str_val_;
	}
	else
		return "";
}

#endif
